package com.example.turismo_pucela;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class RutFragment extends Fragment {
        
        ArrayList<Ruta> rutas = null;
        ArrayList<Ruta> rutasFav = null;
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.rutas, container, false);
                
                LeerXML lector = new LeerXML();
                
                TabHost pestanas = (TabHost) view.findViewById(R.id.tabhostRutas);
        		pestanas.setup();

                //Creamos las pestañas con estilo.
                TabSpec tab1 = pestanas.newTabSpec("Todos");
        		tab1.setContent(R.id.tab1Rutas);
        		View tabIndicator1 = LayoutInflater.from(this.getActivity()).inflate(
        				R.layout.turismovalladolid_tab_indicator_holo, pestanas.getTabWidget(), false);
        		TextView title = (TextView) tabIndicator1
        				.findViewById(android.R.id.title);
        		title.setText("TODOS");
        		tab1.setIndicator(tabIndicator1);

        		TabSpec tab2 = pestanas.newTabSpec("Favoritos");
        		tab2.setContent(R.id.tab2Rutas);
        		View tabIndicator2 = LayoutInflater.from(this.getActivity()).inflate(
        				R.layout.turismovalladolid_tab_indicator_holo, pestanas.getTabWidget(), false);
        		TextView title2 = (TextView) tabIndicator2
        				.findViewById(android.R.id.title);
        		title2.setText("FAVORITOS");
        		tab2.setIndicator(tabIndicator2);

        		pestanas.addTab(tab1);
        		pestanas.addTab(tab2);
                                
                                                        
                
                try {
                	
                	Configuration conf = getResources().getConfiguration();
        			
        			if(conf.locale.toString().equalsIgnoreCase("es")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("en")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-en.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("fr")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-fr.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("ca")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-ca.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("de")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-de.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("eu")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-eu.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("gl")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-gl.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("it")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-it.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("nl")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-nl.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("pt")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-pt.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("ro")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-ro.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("ru")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-ru.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("sv")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-sv.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("tr")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-tr.xml");
        			}else if(conf.locale.toString().equalsIgnoreCase("zh")){
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas-zh.xml");
        			}else {
        				rutas = lector.leerRutas(((MainActivity) getActivity()).getApplicationContext(), "rutas.xml");
        			}
                	
                        
                } catch (XmlPullParserException e) {
                        TextView fallo = new TextView(this.getActivity().getApplicationContext());
                        fallo.setText("Fallo de XML");
                        LinearLayout l = (LinearLayout) view.findViewById(R.id.ly_idiomas);
                        l.addView(fallo);
                        e.printStackTrace();
                } catch (IOException e) {
                        TextView fallo = new TextView(this.getActivity().getApplicationContext());
                        fallo.setText("Fallo de IO");
                        LinearLayout l = (LinearLayout) view.findViewById(R.id.ly_idiomas);
                        l.addView(fallo);
                        e.printStackTrace();
                }
                
                rutasFav = new ArrayList<Ruta>();
                //ArrayAdapter          
                RutasArrayAdapter adaptadorRutas = new RutasArrayAdapter(view.getContext(), 
                                android.R.layout.simple_list_item_1, rutas, rutasFav);
                
                ((ListView) view.findViewById(R.id.listViewRutas)).setAdapter(adaptadorRutas);
                
                
                return view;
        }
}